home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Papers / Garrison / Code / SchmoozingExamples / main.m < prev    next >
Encoding:
Text File  |  2001-06-23  |  2.3 KB  |  75 lines

  1. #import <Foundation/Foundation.h>
  2. #import "IterativeServer.h"
  3. #import "ForkingServer.h"
  4. #import "ThreadedServer.h"
  5. #import <unistd.h>
  6.  
  7. static void usage( const char* program );
  8.  
  9. int main (int argc, const char * argv[])
  10. {
  11.     
  12.     id server = nil;
  13.     unsigned short serverPort = 1701;
  14.     
  15.     // 'server' is a custom object that implements a TCP server using one of
  16.     // the three strategies given on the command line (i.e. iterative,
  17.     // forking, or threading).  
  18.     // 'serverPort' specifies the "well known" port for our service. 
  19.      
  20.     char ch;
  21.     NSString *serverStrategy;
  22.     
  23.     // 'ch' and 'serverStrategy' are a command line processing variables
  24.     
  25.     
  26.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  27.     
  28.     while ( (ch = getopt( argc, argv, "t:")) != -1 ) {
  29.         switch (ch ) {
  30.             case 't':
  31.                 serverStrategy = [NSString stringWithCString:optarg];
  32.                 break;
  33.                 
  34.             default:
  35.                 usage( argv[0] );
  36.                 break;
  37.         }
  38.     
  39.     }
  40.     
  41.     // What server strategy should we run: an iterative server, a forking concurrent
  42.     // server, or a threaded concurrent server?  The command line argument reveals all.
  43.     
  44.     if ( [serverStrategy isEqualToString:@"iterative"] )
  45.         server = [[IterativeServer alloc] initWithLocalPort: serverPort];
  46.     else if ( [serverStrategy isEqualToString:@"forking"] )
  47.         server = [[ForkingServer alloc] initWithLocalPort: serverPort];
  48.     else if ( [serverStrategy isEqualToString:@"threaded"] )
  49.         server = [[ThreadedServer alloc] initWithLocalPort: serverPort];
  50.     else
  51.         usage( argv[0] );
  52.         
  53.     
  54.     [server run];
  55.     // The run method implements the strategy by which the server will process
  56.     // incoming connections.  An iterative server processes one request completely
  57.     // before handling another.  An forking server handles requests concurrently by
  58.     // forking a new process for each request.  A threaded server implements
  59.     // concurrent processing by spawning a new thread for each request.
  60.     
  61.     [server release];
  62.     // Shut down the server and release it from memory.
  63.     
  64.     [pool release];
  65.   
  66.     exit(0);
  67. }
  68.  
  69.  
  70. static void usage( const char* program )
  71. {
  72.     printf("usage: %s -t iterative | forking | threaded", program );
  73.     exit(1);
  74. }
  75.